<?php
/**
 * 接口中心管理
 * @author cjli
 *
 */
class ApiAction extends HomeAction {
    public function _initialize() {
        parent::_initialize();
        global $user;
    }
   	//找回密码发送验证码
	public function found_pwd_code(){
		if((IS_AJAX && IS_POST)){
			try{
				//判断此手机是否已经注册
				$phone = I('post.mobile','intval,trim');
				$user_condition['username']  = $phone;
				$user_condition['phone']  = $phone;
				$user_condition['_logic'] = 'or';
				$result = D("users")->where($user_condition)->find();
				if(!$result){
					$ajaxReturnData['code']=0;
					$ajaxReturnData['message']="该手机号码不是会员，请核对帐号！";
				}else{
					//判断是否超出系统限制（同一天同一号码最多为3条）
					$send_count = $this->getSendTotalCountByPhone($phone,'found');
					if($send_count>3){
						$ajaxReturnData['code']=0;
						$ajaxReturnData['message']="发送失败，已经超过系统上限！";
						$this->ajaxReturn($ajaxReturnData,'json');	exit;
					}
					//判断单个IP是否超出系统限制（同一天同一IP最多为5条）
					$ip = get_client_ip();
					$ip_send_count = $this->getSendTotalCountByIp($ip);
					if($ip_send_count>5){
						$ajaxReturnData['code']=0;
						$ajaxReturnData['message']="发送失败，已经超过系统上限！";
						$this->ajaxReturn($ajaxReturnData,'json');	exit;
					}
					//发送信息
					$code = rand(100000,999999);
					$ShortMessage=A("ShortMessage");
                	$phone_msg=$ShortMessage->send_message('SMS_4225052',$phone,'{"code":"'.$code.'",,"product":"'.$phone.'"}');
					//发送成功
					if($phone_msg){
						//往验证码表添加记录
						$data['mobile']  	= $phone;
						$data['code']		= $code;
						$data['created']	= time();
						$data['ip']			= $ip;
						$data['type']		= 'found';
						SmsCodeModel::add_do($data);
						$ajaxReturnData['code']=200;
						$ajaxReturnData['message']="验证码发送成功！";
					}else{
						$ajaxReturnData['code']=0;
						$ajaxReturnData['message']="发送短信失败！";
					}
				}
			}catch (\Exception $e){
				$ajaxReturnData['code']=0;
				$ajaxReturnData['message']=$e->getMessage();
			}
		}else{
			$ajaxReturnData['code']=0;
			$ajaxReturnData['message']="数据异常，操作失败！";
		}
		$this->ajaxReturn($ajaxReturnData,'json');	
	}
	
   	//注册时发送验证码
    public function code_send(){
		if((IS_AJAX && IS_POST)){
			try{
				//判断此手机是否已经注册
				$phone = I('post.mobile','intval,trim');
				$user_condition['username']  = $phone;
				$user_condition['phone']  = $phone;
				$user_condition['_logic'] = 'or';
				$result = D("users")->where($user_condition)->find();
				if($result){
					$ajaxReturnData['code']=0;
					$ajaxReturnData['message']="该手机号码已经是会员，请直接登录！";
				}else{
					//判断是否超出系统限制（同一天同一号码最多为3条）
					$send_count = $this->getSendTotalCountByPhone($phone,'register');
					if($send_count>3){
						$ajaxReturnData['code']=0;
						$ajaxReturnData['message']="发送失败，已经超过系统上限！";
						$this->ajaxReturn($ajaxReturnData,'json');	exit;
					}
					//判断单个IP是否超出系统限制（同一天同一IP最多为5条）
					$ip = get_client_ip();
					$ip_send_count = $this->getSendTotalCountByIp($ip);
					if($ip_send_count>5){
						$ajaxReturnData['code']=0;
						$ajaxReturnData['message']="发送失败，已经超过系统上限！";
						$this->ajaxReturn($ajaxReturnData,'json');	exit;
					}
					//发送信息
					$code = rand(100000,999999);
					$ShortMessage=A("ShortMessage");
                	$phone_msg=$ShortMessage->send_message('SMS_4225054',$phone,'{"code":"'.$code.'",,"product":"ownonly.net"}');
					//发送成功
					if($phone_msg){
						//往验证码表添加记录
						$data['mobile']  	= $phone;
						$data['code']		= $code;
						$data['created']	= time();
						$data['ip']			= $ip;
						$data['type']		= 'register';
						SmsCodeModel::add_do($data);
						$ajaxReturnData['code']=200;
						$ajaxReturnData['message']="验证码发送成功！";
					}else{
						$ajaxReturnData['code']=0;
						$ajaxReturnData['message']="发送短信失败！";
					}
				}
			}catch (\Exception $e){
				$ajaxReturnData['code']=0;
				$ajaxReturnData['message']=$e->getMessage();
			}
		}else{
			$ajaxReturnData['code']=0;
			$ajaxReturnData['message']="数据异常，注册失败！";
		}
		$this->ajaxReturn($ajaxReturnData,'json');	
	}
	
	private function getSendTotalCountByPhone($phone,$type){
		return SmsCodeModel::get_today_count_by_phone($phone,$type);
	}
	
	private function getSendTotalCountByIp($ip){
		return SmsCodeModel::get_today_count_by_ip($ip);
	}
}

?>